from django.shortcuts import render
from django.views import View

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

from apps.organizations.models import *
from apps.operations.models import UserFavorites
from apps.courses.models import *


# 机构列表
class OrgView(View):
    def get(self, request, *args, **kwargs):
        # 所有机构
        all_org = CourseOrg.objects.all()
        # 机构总数
        org_nums = CourseOrg.objects.count()
        # 所有城市
        city = City.objects.all()
        # 城市总数
        city_nums = City.objects.count()
        # 机构类别分类
        category = ""
        # 城市ID
        city_id = ""

        # 热门机构或所有机构
        nav_tab = request.GET.get("nav_tab", "")
        all_org_hot = all_org.order_by("-students")[:6]
        org_hot_nums = all_org_hot.count()
        category = request.GET.get("category", "")
        # 对用户所需的机构类别进行分类
        if category:
            all_org = all_org.filter(category=category)
            org_nums = all_org.count()

        # 城市ID
        city_id = request.GET.get("city", "")
        if city_id:
            if city_id.isdigit():
                all_org = all_org.filter(city_id=int(city_id))
                org_nums = all_org.count()

        sort = request.GET.get("sort", "")
        if sort == "students":
            all_org = all_org.order_by("-students")
        elif sort == "courses":
            all_org = all_org.order_by("-course_num")

        # 对课程进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(all_org, per_page=8, request=request)
        orgs = p.page(page)

        p_hot = Paginator(all_org_hot, per_page=3, request=request)
        orgs_hot = p_hot.page(page)

        return render(request, 'org_list.html', {
            "all_org": all_org,
            "all_org_hot": all_org_hot,
            "org_nums": org_nums,
            "org_hot_nums": org_hot_nums,
            "city": city,
            "city_nums": city_nums,
            "orgs": orgs,
            "orgs_hot": orgs_hot,
            "category": category,
            "city_id": city_id,
            "nav_tab": nav_tab,
        })


# 机构详情首页
class OrgHomePageView(View):
    def get(self, request, org_id, *args, **kwargs):
        nav_tab = request.GET.get("nav_tab", "")
        # 获取机构
        course_org = CourseOrg.objects.get(id=int(org_id))
        # 点击数+1
        course_org.click_nums += 1
        # 保存更改
        course_org.save()
        # 所有课程
        all_courses = course_org.course_set.all()[:4]
        # 所有教师
        all_teacher = course_org.teacher_set.all()[:4]
        # 课程数
        course_nums = course_org.course_set.all().count()
        # 判断是否收藏
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorites.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2):
                has_fav = True

        return render(request, "org-detail-homepage.html", {
            "nav_tab": nav_tab,
            "course_org": course_org,
            "all_courses": all_courses,
            "all_teacher": all_teacher,
            "course_nums": course_nums,
            "has_fav": has_fav,
        })


# 机构详情讲师页
class OrgTeacherView(View):
    def get(self, request, org_id, *args, **kwargs):
        nav_tab = request.GET.get("nav_tab", "")
        # 获取机构
        course_org = CourseOrg.objects.get(id=int(org_id))
        # 点击数+1
        course_org.click_nums += 1
        # 保存更改
        course_org.save()
        # 所有教师
        all_teacher = course_org.teacher_set.all()
        # 课程数
        course_nums = course_org.course_set.all().count()

        # 对课程进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(all_teacher, per_page=8, request=request)
        teachers = p.page(page)

        # 判断是否收藏
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorites.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2):
                has_fav = True

        return render(request, "org-detail-teachers.html", {
            "nav_tab": nav_tab,
            "course_org": course_org,
            "all_teacher": all_teacher,
            "teachers": teachers,
            "course_nums": course_nums,
            "has_fav": has_fav,
        })


# 机构详情课程页
class OrgCourseView(View):
    def get(self, request, org_id, *args, **kwargs):
        nav_tab = request.GET.get("nav_tab", "")
        # 获取机构
        course_org = CourseOrg.objects.get(id=int(org_id))
        # 点击数+1
        course_org.click_nums += 1
        # 保存更改
        course_org.save()
        # 所有课程
        all_courses = course_org.course_set.all()
        # 课程数
        course_nums = course_org.course_set.all().count()

        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(all_courses, per_page=8, request=request)
        courses = p.page(page)

        # 判断是否收藏
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorites.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2):
                has_fav = True

        return render(request, "org-detail-course.html", {
            "nav_tab": nav_tab,
            "course_org": course_org,
            "all_courses": all_courses,
            "courses": courses,
            "course_nums": course_nums,
            "has_fav": has_fav,
        })


# 机构详情介绍页
class OrgDescView(View):
    def get(self, request, org_id, *args, **kwargs):
        nav_tab = request.GET.get("nav_tab", "")
        # 获取机构
        course_org = CourseOrg.objects.get(id=int(org_id))
        # 点击数+1
        course_org.click_nums += 1
        # 保存更改
        course_org.save()

        # 判断是否收藏
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorites.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2):
                has_fav = True

        return render(request, "org-detail-desc.html", {
            "nav_tab": nav_tab,
            "course_org": course_org,
            "has_fav": has_fav,
        })


# 讲师列表
class TeacherListView(View):
    def get(self, request, *args, **kwargs):
        # 所有讲师
        all_teacher = Teacher.objects.all()
        # 讲师总数
        teacher_nums = Teacher.objects.count()

        # 热门讲师或所有讲师
        nav_tab = request.GET.get("nav_tab", "")
        all_teacher_hot = all_teacher.order_by("-fav_nums")[:6]

        teacher_hot_nums = all_teacher_hot.count()

        sort = request.GET.get("sort", "")
        if sort == "fav_nums":
            all_teacher = all_teacher.order_by("-fav_nums")
        elif sort == "courses":
            all_teacher = all_teacher.order_by("-course_num")

        # 对课程进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(all_teacher, per_page=8, request=request)
        teachers = p.page(page)

        p_hot = Paginator(all_teacher_hot, per_page=3, request=request)
        teachers_hot = p_hot.page(page)

        return render(request, 'teacher_list.html', {
            "all_teacher": all_teacher,
            "all_teacher_hot": all_teacher_hot,
            "teacher_nums": teacher_nums,
            "teacher_hot_nums": teacher_hot_nums,
            "teachers": teachers,
            "teachers_hot": teachers_hot,
            "nav_tab": nav_tab,
        })


# 讲师详情首页
class TeacherDetailView(View):
    def get(self, request, teacher_id, *args, **kwargs):
        teacher = Teacher.objects.get(id=int(teacher_id))

        teacher_fav = False
        org_fav = False
        # 判断是否收藏
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorites.objects.filter(user=request.user, fav_id=teacher.id, fav_type=3):
                has_fav = True

        hot_teachers = Teacher.objects.all().order_by("-click_nums")[:3]
        return render(request, "teacher-detail-homepage.html", {
            "teacher": teacher,
            "teacher_fav": teacher_fav,
            "org_fav": org_fav,
            "hot_teachers": hot_teachers,
            "has_fav": has_fav,
        })
